package com.training.food.controller;

import com.training.food.AES.AES;
import com.training.food.Domain.RecordResult;
import com.training.food.Mapper.UserMapper;
import com.training.food.pojo.Examine;
import com.training.food.pojo.Recordinfo;
import com.training.food.pojo.User;
import com.training.food.service.LoginService;
import com.training.food.service.ExamineService;
import com.training.food.service.RecordService;
import com.training.food.service.RegisterService;
import com.training.food.util.CommonResult;
import com.training.food.util.LoginPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.time.Duration;
import java.util.UUID;
@RestController
public class UserController {
    @Resource
    RegisterService registerService;
    @Resource
    LoginService loginService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    //用户注册
    @PostMapping(path ="/food/user/register")
    CommonResult register(@RequestBody User user) throws Exception {
        user.setPassword(AES.encryptionAES(user.getPassword()));
        if(registerService.save(user)){
            return CommonResult.success("注册成功");
        }
        return CommonResult.failed("用户已存在");
    }
    //免登陆
    @PostMapping(path = "/food/user/freeLogin")
    LoginPojo freeLogin(HttpServletRequest request){
        String token= (String) request.getAttribute("user");
        //System.out.println("theToken:"+token);

        Object user1=redisTemplate.opsForValue().get(token);
        if(user1!=null) {
            User user2 = (User) user1;
            LoginPojo loginPojo=new LoginPojo();
            //System.out.println(user2);
            loginPojo.setCommonResult(CommonResult.success(token));
            loginPojo.setPower(user2.getPower());
            loginPojo.setState(user2.getState());
            return loginPojo;

        }
        LoginPojo loginPojo=new LoginPojo();
        loginPojo.setCommonResult(CommonResult.failed("登录失败"));
        loginPojo.setState('-');
        loginPojo.setPower('-');
        return loginPojo;
    }
    //查看用户的原密码
    @PostMapping(path = "/food/user/getPassword")
    public CommonResult getPassword(@RequestBody User user) throws Exception {
        String password=AES.decipheringAES(user.getPassword());
        return CommonResult.success(password);
    }
    //用户登录
    @PostMapping(path = "/food/user/login")
    LoginPojo login(@RequestBody User user) throws Exception {
        user.setPassword(AES.encryptionAES(user.getPassword()));
        User user1=loginService.login(user);
        if (user1!=null){
            //生成token令牌
            String token= UUID.randomUUID()+"";
            //存到redis数据库
            //System.out.println("theUser:"+user1);
            loginService.change(user);
            if(!user.isNoLogin()) {

                redisTemplate.opsForValue().set(token, user1, Duration.ofHours(1L));
            }
            else{

                redisTemplate.opsForValue().set(token, user1, Duration.ofDays(7L));
            }
            LoginPojo loginPojo=new LoginPojo();
            loginPojo.setCommonResult(CommonResult.success(token));
            loginPojo.setPower(user1.getPower());
            loginPojo.setState(user1.getState());
            return loginPojo;
        }
        LoginPojo loginPojo=new LoginPojo();
        loginPojo.setCommonResult(CommonResult.failed("登录失败"));
        loginPojo.setState('-');
        loginPojo.setPower('-');
        return loginPojo;

    }
    //用户冻结
    @PostMapping("/food/freeze")
    public CommonResult freeze(@RequestBody User user){
        //System.out.println(user);
        if (loginService.select(user)!=null){
            loginService.freeze(user);
            return CommonResult.success("成功");
        }
        return CommonResult.failed("未找到用户");
    }
    //修改用户密码
    @PostMapping("/food/user/updatePassword")
    public CommonResult updatePassword(HttpServletRequest request, @RequestBody User user) throws Exception {
        String token= (String) request.getAttribute("user");
        //System.out.println("theToken:"+token);

        Object user1=redisTemplate.opsForValue().get(token);
        User user2=(User)user1;
        //System.out.println(user2);
        user2.setPassword(AES.encryptionAES(user.getPassword()));

        if(registerService.findByUsername(user2.getUsername())){
            registerService.update(user2);
            return CommonResult.success("修改用户密码成功");
        }
        return CommonResult.failed("未获取到用户");
    }
    //根据token返回用户信息
    @PostMapping("/food/user/getByToken")
    public CommonResult getByToken(HttpServletRequest request){
        String token= (String) request.getAttribute("user");
        //System.out.println("theToken:"+token);

        Object user1=redisTemplate.opsForValue().get(token);
        User user2=(User)user1;

        return CommonResult.success(user2);
    }
    //获取用户的登录状态
    @PostMapping("/food/user/getUserOfLogin")
    public CommonResult getUserOfLogin(HttpServletRequest request)throws UnsupportedEncodingException{
       //获取headers中的参数
        String token= (String) request.getAttribute("user");
        Object user=redisTemplate.opsForValue().get(token);
        if(user!=null){
            return CommonResult.success(user);
        }
        return CommonResult.failed("未获取到用户");
    }




}
